package com.ct.ah.lab.controller;

import com.ct.ah.lab.entity.Address;
import com.ct.ah.lab.entity.User;
import com.ct.ah.lab.exception.UserException;
import com.ct.ah.lab.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/addresses")
public class AddressController {
    @Autowired
    private IAddressService addressService;


    @RequestMapping("/createAddress")
    @ResponseBody
    public Map<String, Object> createAddress(Address address, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");

        if (user != null) {
            try {
                address.setUid(user.getUid());
                addressService.saveAddress(address);
                map.put("state", 1000);
                map.put("data", "success");
            } catch (Exception e) {
                map.put("msg", "get internal error");
            }
        } else {
            map.put("msg", "not login");
        }

        return map;
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");

        if (user != null) {
            List<Address> addresses = addressService.list(user.getUid());
            map.put("state", 1000);
            map.put("data", addresses);
        } else {
            map.put("msg", "not login");
        }

        return map;
    }


    @RequestMapping("/getDefault")
    @ResponseBody
    public Map<String, Object> getDefault(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");

        if (user != null) {
            Address addresses = addressService.getDefault(user.getUid());
            map.put("state", 1000);
            map.put("data", addresses);
        } else {
            map.put("msg", "not login");
        }
        return map;
    }


    //getDefault


    @RequestMapping("/{aid}/set_default")
    @ResponseBody
    public Map<String, Object> set_default(@PathVariable Integer aid, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");

        if (user != null) {
            List<Address> addresses = addressService.list(user.getUid());
            for (Address address : addresses
            ) {
                if ((address.getIsDefault() == null || address.getIsDefault() != 0) && address.getAid() != aid) {
                    addressService.setAddressNotDefault(address.getAid());
                }
                addressService.setAddressDefault(aid);

            }

            map.put("state", 1000);
        } else {
            map.put("msg", "not login");
        }

        return map;
    }

    @RequestMapping("/{aid}/delete")
    @ResponseBody
    public Map<String, Object> deleteByAid(@PathVariable Integer aid, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");

        if (user != null) {
            addressService.deleteByAid(aid);
            map.put("state", 1000);
        } else {
            map.put("msg", "not login");
        }

        return map;
    }

    @RequestMapping("/{aid}/updateAddressInfo")
    @ResponseBody
    public Map<String,Object> updateAddressInfo(Address address,HttpServletRequest req) {
        Map<String,Object> map = new HashMap<>();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");

        if (user != null) {

            try {
                addressService.changeAddressInfo(address);

                List<Address> addresses = addressService.list(user.getUid());
                for (Address address_i : addresses
                ) {
                    if ((address_i.getIsDefault() == null || address_i.getIsDefault() != 0) && address_i.getAid() != address.getAid()) {
                        addressService.setAddressNotDefault(address_i.getAid());
                    }
                }


                map.put("state", 1000);
                map.put("data", "success");
            } catch (Exception e) {
                map.put("msg", "get internal error");
            }
        } else {
            map.put("msg", "not login");
        }
        return map;
    }

    @RequestMapping("/{aid}/getAddressInfo")
    @ResponseBody
    public Map<String, Object> getByAid(@PathVariable Integer aid,HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");

        if (user != null) {
            Address address = addressService.getByAid(aid);

            map.put("state", 1000);
            map.put("data", address);
        } else {
            map.put("msg", "not login");
        }
        return map;
    }

}
